The \textit{Nintendo Entertainment System} is an 8-bit cartridge-based video 
game console first launched in 1983 to instant acclaim. NES quickly became the 
best selling game console and helped revitalize the rapidly declining home gaming 
industry. \cite{wikipediaNES} It was a third generation console consisting of an 8-bit MOS 
Technology 6502 microprocessor produced by Ricoh which incorporated custom sound 
processing hardware on-die in addition to a \textit{direct memory access controller}
(DMA). The \textit{Central Processing Unit} (CPU) ran at either 1.79MHz or 1.66MHz 
depending if the designated region was using either NTSC (US) or PAL (EU) systems 
respectivly. Graphics were taken care of by the \textit{Picture Processing Unit} 
(PPU) also manufactured by Ricoh. The picture processor could display a maximum 
of 53 colors and had a resolution of 256x240 pixels.
The built-in \textit{Audio Processing Unit} (APU) had support for five sound 
channels with a volume control of 16 levels.

In addition the system contains a mediocre 2KB of \textit{random access memory} 
(RAM) - connected to a 16bit adress and 8bit data bus respectively - which can be 
expanded by hardware contained in the game cartridges which of course also holds 
the necessary media in \textit{read only memory} (ROM). \cite{nintech} \\

Using Haskell was the first thing we agreed upon in this project and made the 
modeling easy since the language is statically typed and has type inference. 
Because of that, a lot of small mistakes and bugs are caught at compile-time 
instead of crashing the program at runtime.

Furthermore Haskell uses lazy evaluation which means that no values are computed 
unless they are actually needed. It follows from the laziness that one may write 
programs without actually knowing some values; for example in Haskell one can use 
infinite lists, a feature which is heavily used in our implementation. Since 
Haskell is a pure language, neither side effects nor variables in the classical 
programming sense are permitted. This is problematic due to the highly stateful 
characteristics of a CPU. To handle that, the need for monads arise. Monads 
provide a way to run actions in a sequential manner, encapsulating possible side 
effects such as a state. \\

Emulation is the art of imitating a system on another system be it either 
hardware or software. The interesting part of emulation is being able to reproduce 
the exact behaviour of the actual system; in our case the NES. Emulation is also 
a way of preserving an obsolete system since it has been out of production for many 
years but convenient in the sense that a working emulator can run any program 
written for the NES.\cite{emulation}
 
\section{Motivation}
Since the NES emerged, emulation has been on the rise. Most emulators however, 
are written in ASM/C and are highly optimized for speed. The fact is that many 
were written a long time ago when computers were not as powerful as they are these 
days. There are several consequences from that, most consequential being that the 
code looks awful and is not readable at all. The advantage when using Haskell is 
that the high abstraction level in the language makes it easy to model complex 
structures in a readable manner. Another motivating factor is that there are no 
NES emulators written in Haskell, at least to our knowledge. 

\section{Purpose \& Goals}
The goal of this project is to show that Haskell can be used in practical 
applications by implementing an emulator for the \textit{Nintendo Entertainment System}, 
that is able to run a small subset of games, in a clean and readable way by using 
Haskell's powerful type system and abstraction for modelling the NES hardware. \\

Since writing a complete emulator is very hard and a tedious process, we have 
decided on some sub-goals due to the time constraints for this project. \\
We have worked on the project in the following steps:
\begin{enumerate}
    \item
        Emulation of the CPU is considered complete when it follows the 
        specification of the 2A03[SPECS]. It should be able to run all supported 
        instructions and return a "correct" state.
    \item
        Emulation of the PPU is considered done when the emulated PPU behaves as 
        the one in the real NES hardware would, that is when the graphic outputted 
        to the screen is the expected one.
    \item 
        Synchronization between CPU and PPU is considered to work when a PPU 
        write to a register is reflected in the corresponding CPU memory, and vice 
        versa.
    \item 
        DMA is considered to work when it takes 512 cycles to move 256 bytes 
        from CPU memory to PPU memory.
\end{enumerate}

\section{Development Cycle}
We have followed a simple scheme for developing the application. It is always a 
good idea to start with research on the subject. To test and debug is also 
central to any programming project. \cite{waterfall}
\begin{figure}[h!]
    \begin{center}
        \includegraphics[width=200px]{images/waterfall.png}
        \caption{Modified waterfall model}
    \end{center}
\end{figure}
